package com.lvmama.pangolin.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.pangolin.HBaseOper.*;
import com.lvmama.pangolin.common.Constant;
import com.lvmama.pangolin.common.DateUtil;
import com.lvmama.pangolin.common.RowkeyGenerator;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.ValidateProductListConditionVO;
import com.lvmama.pangolin.entity.mt.MTTicketCommodityItem;
import com.lvmama.pangolin.entity.mt.MTTicket_CommodityDetail;
import com.lvmama.pangolin.entity.qnr.QNRTicketCommodityItem;
import com.lvmama.pangolin.entity.qnr.QNRTicket_CommodityDetail;
import com.lvmama.pangolin.entity.tc.TCTicket_CommodityDetail;
import com.lvmama.pangolin.entity.tc.TCommodityItem;
import com.lvmama.pangolin.entity.tn.TNTicketCommodityItem;
import com.lvmama.pangolin.entity.tn.TNTicket_CommodityDetail;
import com.lvmama.pangolin.entity.xc.XCTicketCommodityItem;
import com.lvmama.pangolin.entity.xc.XCTicket_CommodityDetail;
import com.lvmama.pangolin.mt.service.MTTicketCommodityDetailService;
import com.lvmama.pangolin.mt.service.MTTicketCommodityItemsService;
import com.lvmama.pangolin.qnr.service.QNRTicketCommodityDetailService;
import com.lvmama.pangolin.qnr.service.QNRTicketCommodityItemsService;
import com.lvmama.pangolin.tc.service.TCCommodityItemsService;
import com.lvmama.pangolin.tc.service.TCTicketCommodityDetailService;
import com.lvmama.pangolin.tn.service.TNTicketCommodityDetailService;
import com.lvmama.pangolin.tn.service.TNTicketCommodityItemsService;
import com.lvmama.pangolin.xc.service.XCTicketCommodityDetailService;
import com.lvmama.pangolin.xc.service.XCTicketCommodityItemsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("validateProduct")
public class ValidateProductController {

    private static Logger log = Logger.getLogger(ValidateProductController.class);

    @Autowired
    TCTicketCommodityDetailService TCTicketCommodityDetailService;
    @Autowired
    TNTicketCommodityDetailService TNTicketCommodityDetailService;
    @Autowired
    XCTicketCommodityDetailService XCTicketCommodityDetailService;
    @Autowired
    QNRTicketCommodityDetailService QNRTicketCommodityDetailService;
    @Autowired
    MTTicketCommodityDetailService MTTicketCommodityDetailService;
    @Autowired
    TCCommodityItemsService TCCommodityItemsService;
    @Autowired
    XCTicketCommodityItemsService XCTicketCommodityItemsService;
    @Autowired
    TNTicketCommodityItemsService TNTicketCommodityItemsService;
    @Autowired
    QNRTicketCommodityItemsService QNRTicketCommodityItemsService;
    @Autowired
    MTTicketCommodityItemsService MTTicketCommodityItemsService;

    @Autowired
    TCHBEntityOperation TCHBEntityOperation;
    @Autowired
    TNHBEntityOperation TNHBEntityOperation;
    @Autowired
    XCHBEntityOperation XCHBEntityOperation;
    @Autowired
    QNRHBEntityOperation QNRHBEntityOperation;
    @Autowired
    MTHBEntityOperation MTHBEntityOperation;


    @RequestMapping("TCValidateProductList")
    public String TCValidateProductList(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map = getTicketListByVO(map, validateProductListConditionVO);
        PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
        PageInfo<TCTicket_CommodityDetail> page = new PageInfo<TCTicket_CommodityDetail>(TCTicketCommodityDetailService.queryLogByMap(map));
        List<TCTicket_CommodityDetail> TCTicket_CommodityDetailList = page.getList();
        model.addAttribute("page", page);
        model.addAttribute("TCTicket_CommodityDetailList", TCTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/TCValidateProdcuct";

    }

    @RequestMapping(value = "/TicketEdit", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> TicketEdit(ModelMap model, TCTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<String, Object>(1);
        int code = 1;
        try {
            ticket = getTCTicketByID(ticket);
        } catch (Exception e) {
            log.error("validateProduct...TicketEdit memthod error", e);
            code = 0;
        }
        map.put("code", code);
        map.put("ticketVO", ticket);
        return map;

    }


    @RequestMapping("ticketAdd")
    @ResponseBody
    public Map<String, Object> ticketAdd(ModelMap model, HttpServletRequest request, TCTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        List<TCTicket_CommodityDetail> TCTicket_CommodityDetailList = null;
        PageInfo<TCTicket_CommodityDetail> page = null;
        Date nowDate = new Date();
        try {
            if (StringTools.isValid(ticket.getDetail_id())) {
                ticket.setUpdate_time(nowDate);
                TCTicketCommodityDetailService.updateLog(ticket);
            }
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            map = getTicketListByVO(map, validateProductListConditionVO);
            page = new PageInfo<TCTicket_CommodityDetail>(TCTicketCommodityDetailService.queryLogByMap(map));
            TCTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("validateProduct...ticketAdd memthod error", e);
            map.put("code", "-1");
        }
        model.addAttribute("page", page);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        model.addAttribute("TCTicket_CommodityDetailList", TCTicket_CommodityDetailList);
        return map;
    }

    @RequestMapping("deleteOneTicket")
    public String deleteOneTicket(ModelMap model, HttpServletRequest request, TCTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        deleteTCTicketByID(ticket);
        map = getTicketListByVO(map, validateProductListConditionVO);
        List<TCTicket_CommodityDetail> TCTicket_CommodityDetailList = null;
        PageInfo<TCTicket_CommodityDetail> page = null;
        try {
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<TCTicket_CommodityDetail>(TCTicketCommodityDetailService.queryLogByMap(map));
            TCTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...deleteOneTicket...method...queryLogByMap...error", e);
            map.put("code", "-1");
        }
        model.addAttribute("page", page);
        model.addAttribute("TCTicket_CommodityDetailList", TCTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/TCValidateProdcuct";
    }

    @RequestMapping("saveOneTicket")
    @ResponseBody
    public Map<String, Object> saveOneTicket(ModelMap model, HttpServletRequest request, TCTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        ticket = getTCTicketByID(ticket);
        boolean isSave = false;
        try {
            TCommodityItem TCommodityItem = TCCommodityItemsService.selectByProdCommId(ticket.getProduct_id(), ticket.getCommodity_id());
            //当价格校验失败表中的日期大于等于商品表中的日期时候才更新商品表中该条商品
            if (TCommodityItem != null) {
                if (TCommodityItem.getNew_grab_time() != null && DateUtil.compare_date(ticket.getCreate_time(), TCommodityItem.getNew_grab_time()) > 0) {
                    if (TCommodityItem.getCommodity_type().equals(ticket.getCommodity_type())) {    //确保 商品id 产品id 以及 商品类型 都相同
                        TCCommodityItemsService.updateByProdCommIdAndType(ticket);
                    }
                }
            }
            isSave = TCHBEntityOperation.saveTCTicketCommodity(ticket, ticket.getDetail_id(), Constant.TICKET_PRODUCT_H_TC, Constant.HB_VARIABLE_FAMILY);
            if (isSave) {
                deleteTCTicketByID(ticket);
            }
            map = getTicketListByVO(map, validateProductListConditionVO);
            List<TCTicket_CommodityDetail> TCTicket_CommodityDetailList = null;
            PageInfo<TCTicket_CommodityDetail> page = null;
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<TCTicket_CommodityDetail>(TCTicketCommodityDetailService.queryLogByMap(map));
            TCTicket_CommodityDetailList = page.getList();
            model.addAttribute("page", page);
            model.addAttribute("TCTicket_CommodityDetailList", TCTicket_CommodityDetailList);
        } catch (Exception e) {
            log.error("ValidateProductController...saveOneTicket...method..error", e);
            map.put("code", "-1");
        }
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return map;
    }

    @RequestMapping("TicketDeleteBatch")
    public String TicketDeleteBatch(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) {
        Map<String, Object> map = new HashMap<String, Object>();
        map = getTicketListByVO(map, validateProductListConditionVO);
        try {
            TCTicketCommodityDetailService.deleteLogByMap(map); //删除全部符合条件数据
        } catch (Exception e) {
            log.error("ValidateProductController...TicketDeleteBatch...method...deleteLogByMap...error ", e);
        }

        PageInfo<TCTicket_CommodityDetail> page = null;
        List<TCTicket_CommodityDetail> TCTicket_CommodityDetailList = null;
        try {
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<TCTicket_CommodityDetail>(TCTicketCommodityDetailService.queryLogByMap(map));
            TCTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...TicketDeleteBatch...method...TCTicketCommodityDetailService...error", e);
        }
        model.addAttribute("page", page);
        model.addAttribute("TCTicket_CommodityDetailList", TCTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/TCValidateProdcuct";
    }

    @RequestMapping("TicketSaveBatch")
    public String TicketSaveBatch(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<TCTicket_CommodityDetail> page = null;
        List<TCTicket_CommodityDetail> TCTicket_CommodityDetailList = null;
        map = getTicketListByVO(map, validateProductListConditionVO);
        try {
            List<TCTicket_CommodityDetail> ticketList = TCTicketCommodityDetailService.queryLogByMap(map);
            if (null != ticketList && ticketList.size() > 0) {

                for (TCTicket_CommodityDetail ticket : ticketList) {

                    TCommodityItem TCommodityItem = TCCommodityItemsService.selectByProdCommId(ticket.getProduct_id(), ticket.getCommodity_id());
                    if (TCommodityItem != null) {
                        //当价格校验失败表中的日期大于等于商品表中的日期时候才更新商品表中该条商品
                        if (TCommodityItem.getNew_grab_time() != null && DateUtil.compare_date(ticket.getCreate_time(), TCommodityItem.getNew_grab_time()) > 0) {
                            if (TCommodityItem.getCommodity_type().equals(ticket.getCommodity_type())) {    //确保 商品id 产品id 以及 商品类型 都相同
                                TCCommodityItemsService.updateByProdCommIdAndType(ticket);
                            }
                        }
                    }
                    // 入hbase
                    TCHBEntityOperation.saveTCTicketCommodity(ticket, ticket.getDetail_id(), Constant.TICKET_PRODUCT_H_TC, Constant.HB_VARIABLE_FAMILY);
                }
            }
            TCTicketCommodityDetailService.deleteLogByMap(map); //删除mysql库中符合条件数据
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<TCTicket_CommodityDetail>(TCTicketCommodityDetailService.queryLogByMap(map));
            TCTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...TicketSaveBatch...method...TCTicketCommodityDetailService...error", e);
        }
        model.addAttribute("page", page);
        model.addAttribute("TCTicket_CommodityDetailList", TCTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/TCValidateProdcuct";

    }

    @RequestMapping("XCValidateProductList")
    public String XCValidateProductList(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map = getTicketListByVO(map, validateProductListConditionVO);
        PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
        PageInfo<XCTicket_CommodityDetail> page = new PageInfo<XCTicket_CommodityDetail>(XCTicketCommodityDetailService.queryLogByMap(map));
        List<XCTicket_CommodityDetail> XCTicket_CommodityDetailList = page.getList();
        model.addAttribute("page", page);
        model.addAttribute("XCTicket_CommodityDetailList", XCTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/XCValidateProdcuct";

    }

    @RequestMapping(value = "/XCTicketEdit", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> XCTicketEdit(ModelMap model, XCTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<String, Object>(1);
        int code = 1;
        try {
            ticket = getXCTicketByID(ticket);
        } catch (Exception e) {
            log.error("validateProduct...XCTicketEdit memthod error", e);
            code = 0;
        }
        map.put("code", code);
        map.put("ticketVO", ticket);
        return map;

    }

    @RequestMapping("XCTicketAdd")
    @ResponseBody
    public Map<String, Object> XCTicketAdd(ModelMap model, HttpServletRequest request, XCTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        List<XCTicket_CommodityDetail> XCTicket_CommodityDetailList = null;
        PageInfo<XCTicket_CommodityDetail> page = null;
        Date nowDate = new Date();
        try {
            if (StringTools.isValid(ticket.getDetail_id())) {
                ticket.setUpdate_time(nowDate);
                XCTicketCommodityDetailService.updateLog(ticket);
            }
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            map = getTicketListByVO(map, validateProductListConditionVO);
            page = new PageInfo<XCTicket_CommodityDetail>(XCTicketCommodityDetailService.queryLogByMap(map));
            XCTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("validateProduct...XCTicketAdd memthod error", e);
            map.put("code", "-1");
        }
        model.addAttribute("page", page);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        model.addAttribute("XCTicket_CommodityDetailList", XCTicket_CommodityDetailList);
        return map;
    }

    @RequestMapping("deleteXCOneTicket")
    public String deleteXCOneTicket(ModelMap model, HttpServletRequest request, XCTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        deleteXCTicketByID(ticket);
        map = getTicketListByVO(map, validateProductListConditionVO);
        List<XCTicket_CommodityDetail> XCTicket_CommodityDetailList = null;
        PageInfo<XCTicket_CommodityDetail> page = null;
        try {
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<XCTicket_CommodityDetail>(XCTicketCommodityDetailService.queryLogByMap(map));
            XCTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...deleteXCOneTicket...method...queryLogByMap...error", e);
            map.put("code", "-1");
        }
        model.addAttribute("page", page);
        model.addAttribute("XCTicket_CommodityDetailList", XCTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/XCValidateProdcuct";
    }


    @RequestMapping("saveXCOneTicket")
    @ResponseBody
    public Map<String, Object> saveXCOneTicket(ModelMap model, HttpServletRequest request, XCTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        ticket = getXCTicketByID(ticket);
        boolean isSave = false;
        try {

            XCTicketCommodityItem XCTicketCommodityItem = XCTicketCommodityItemsService.selectByProdCommId(ticket.getProduct_id(), ticket.getCommodity_id());
            //当价格校验失败表中的日期大于等于商品表中的日期时候才更新商品表中该条商品
            if (null != XCTicketCommodityItem) {
                if (XCTicketCommodityItem.getNew_grab_time() != null && DateUtil.compare_date(ticket.getCreate_time(), XCTicketCommodityItem.getNew_grab_time()) > 0) {
                    if (XCTicketCommodityItem.getCommodity_type().equals(ticket.getCommodity_type())) {    //确保 商品id 产品id 以及 商品类型 都相同
                        XCTicketCommodityItemsService.updateByProdCommIdAndType(ticket);
                    }
                }
            }

            isSave = XCHBEntityOperation.saveXCTicketCommodity(ticket, ticket.getDetail_id(), Constant.TICKET_PRODUCT_H_TC, Constant.HB_VARIABLE_FAMILY);
            if (isSave) {
                deleteXCTicketByID(ticket);
            }
            map = getTicketListByVO(map, validateProductListConditionVO);
            List<XCTicket_CommodityDetail> XCTicket_CommodityDetailList = null;
            PageInfo<XCTicket_CommodityDetail> page = null;
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<XCTicket_CommodityDetail>(XCTicketCommodityDetailService.queryLogByMap(map));
            XCTicket_CommodityDetailList = page.getList();
            model.addAttribute("page", page);
            model.addAttribute("XCTicket_CommodityDetailList", XCTicket_CommodityDetailList);
        } catch (Exception e) {
            log.error("ValidateProductController...saveXCOneTicket...method..error", e);
            map.put("code", "-1");
        }
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return map;
    }

    @RequestMapping("XCTicketDeleteBatch")
    public String XCTicketDeleteBatch(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) {
        Map<String, Object> map = new HashMap<String, Object>();
        map = getTicketListByVO(map, validateProductListConditionVO);
        try {
            XCTicketCommodityDetailService.deleteLogByMap(map); //删除全部符合条件数据
        } catch (Exception e) {
            log.error("ValidateProductController...XCTicketDeleteBatch...method...deleteLogByMap...error", e);
        }

        PageInfo<XCTicket_CommodityDetail> page = null;
        List<XCTicket_CommodityDetail> XCTicket_CommodityDetailList = null;
        try {
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<XCTicket_CommodityDetail>(XCTicketCommodityDetailService.queryLogByMap(map));
            XCTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...XCTicketDeleteBatch...method...XCTicketCommodityDetailService...error", e);
        }
        model.addAttribute("page", page);
        model.addAttribute("XCTicket_CommodityDetailList", XCTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/XCValidateProdcuct";
    }

    @RequestMapping("XCTicketSaveBatch")
    public String XCTicketSaveBatch(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<XCTicket_CommodityDetail> page = null;
        List<XCTicket_CommodityDetail> XCTicket_CommodityDetailList = null;
        map = getTicketListByVO(map, validateProductListConditionVO);
        try {
            List<XCTicket_CommodityDetail> ticketList = XCTicketCommodityDetailService.queryLogByMap(map);
            if (null != ticketList && ticketList.size() > 0) {

                for (XCTicket_CommodityDetail ticket : ticketList) {

                    XCTicketCommodityItem XCTicketCommodityItem = XCTicketCommodityItemsService.selectByProdCommId(ticket.getProduct_id(), ticket.getCommodity_id());
                    if (null != XCTicketCommodityItem) {
                        //当价格校验失败表中的日期大于等于商品表中的日期时候才更新商品表中该条商品
                        if (XCTicketCommodityItem.getNew_grab_time() != null && DateUtil.compare_date(ticket.getCreate_time(), XCTicketCommodityItem.getNew_grab_time()) > 0) {
                            if (XCTicketCommodityItem.getCommodity_type().equals(ticket.getCommodity_type())) {    //确保 商品id 产品id 以及 商品类型 都相同
                                XCTicketCommodityItemsService.updateByProdCommIdAndType(ticket);
                            }
                        }
                    }
                    // 入hbase
                    XCHBEntityOperation.saveXCTicketCommodity(ticket, ticket.getDetail_id(), Constant.TICKET_PRODUCT_H_TC, Constant.HB_VARIABLE_FAMILY);
                }
            }
            XCTicketCommodityDetailService.deleteLogByMap(map); //删除mysql库中符合条件数据
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<XCTicket_CommodityDetail>(XCTicketCommodityDetailService.queryLogByMap(map));
            XCTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...XCTicketSaveBatch...method..XCTicketCommodityDetailService...error", e);
        }
        model.addAttribute("page", page);
        model.addAttribute("XCTicket_CommodityDetailList", XCTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/XCValidateProdcuct";

    }

    @RequestMapping("TNValidateProductList")
    public String TNValidateProductList(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map = getTicketListByVO(map, validateProductListConditionVO);
        PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
        PageInfo<TNTicket_CommodityDetail> page = new PageInfo<TNTicket_CommodityDetail>(TNTicketCommodityDetailService.queryLogByMap(map));
        List<TNTicket_CommodityDetail> TNTicket_CommodityDetailList = page.getList();
        model.addAttribute("page", page);
        model.addAttribute("TNTicket_CommodityDetailList", TNTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/TNValidateProdcuct";

    }

    @RequestMapping(value = "/TNTicketEdit", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> TicketEdit(ModelMap model, TNTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<String, Object>(1);
        int code = 1;
        try {
            ticket = getTNTicketByID(ticket);
        } catch (Exception e) {
            log.error("validateProduct...TNTicketEdit memthod error", e);
            code = 0;
        }
        map.put("code", code);
        map.put("ticketVO", ticket);
        return map;

    }

    @RequestMapping("TNTicketAdd")
    @ResponseBody
    public Map<String, Object> TNTicketAdd(ModelMap model, HttpServletRequest request, TNTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        List<TNTicket_CommodityDetail> TNTicket_CommodityDetailList = null;
        PageInfo<TNTicket_CommodityDetail> page = null;
        Date nowDate = new Date();
        try {
            if (StringTools.isValid(ticket.getDetail_id())) {
                ticket.setUpdate_time(nowDate);
                TNTicketCommodityDetailService.updateLog(ticket);
            }
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            map = getTicketListByVO(map, validateProductListConditionVO);
            page = new PageInfo<TNTicket_CommodityDetail>(TNTicketCommodityDetailService.queryLogByMap(map));
            TNTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("validateProduct...TNTicketAdd memthod error", e);
            map.put("code", "-1");
        }
        model.addAttribute("page", page);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        model.addAttribute("TNTicket_CommodityDetailList", TNTicket_CommodityDetailList);
        return map;
    }

    @RequestMapping("deleteTNOneTicket")
    public String deleteTNOneTicket(ModelMap model, HttpServletRequest request, TNTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        deleteTNTicketByID(ticket);
        map = getTicketListByVO(map, validateProductListConditionVO);
        List<TNTicket_CommodityDetail> TNTicket_CommodityDetailList = null;
        PageInfo<TNTicket_CommodityDetail> page = null;
        try {
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<TNTicket_CommodityDetail>(TNTicketCommodityDetailService.queryLogByMap(map));
            TNTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...deleteTNOneTicket...method...queryLogByMap...error", e);
            map.put("code", "-1");
        }
        model.addAttribute("page", page);
        model.addAttribute("TNTicket_CommodityDetailList", TNTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/TNValidateProdcuct";
    }


    @RequestMapping("saveTNOneTicket")
    @ResponseBody
    public Map<String, Object> saveTNOneTicket(ModelMap model, HttpServletRequest request, TNTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        ticket = getTNTicketByID(ticket);
        boolean isSave = false;
        try {
            TNTicketCommodityItem TNTicketCommodityItem = TNTicketCommodityItemsService.selectByProdCommId(ticket.getProduct_id(), ticket.getCommodity_id());
            //当价格校验失败表中的日期大于等于商品表中的日期时候才更新商品表中该条商品
            if (null != TNTicketCommodityItem) {
                if (TNTicketCommodityItem.getNew_grab_time() != null && DateUtil.compare_date(ticket.getCreate_time(), TNTicketCommodityItem.getNew_grab_time()) > 0) {
                    if (TNTicketCommodityItem.getCommodity_type().equals(ticket.getCommodity_type())) {    //确保 商品id 产品id 以及 商品类型 都相同
                        TNTicketCommodityItemsService.updateByProdCommIdAndType(ticket);
                    }
                }
            }

            isSave = TNHBEntityOperation.saveTNTicketCommodity(ticket, ticket.getDetail_id(), Constant.TICKET_PRODUCT_H_TC, Constant.HB_VARIABLE_FAMILY);
            if (isSave) {
                deleteTNTicketByID(ticket);
            }
            map = getTicketListByVO(map, validateProductListConditionVO);
            List<TNTicket_CommodityDetail> TNTicket_CommodityDetailList = null;
            PageInfo<TNTicket_CommodityDetail> page = null;
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<TNTicket_CommodityDetail>(TNTicketCommodityDetailService.queryLogByMap(map));
            TNTicket_CommodityDetailList = page.getList();
            model.addAttribute("page", page);
            model.addAttribute("TNTicket_CommodityDetailList", TNTicket_CommodityDetailList);
        } catch (Exception e) {
            log.error("ValidateProductController...saveTNOneTicket...method..error", e);
            map.put("code", "-1");
        }
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return map;
    }

    @RequestMapping("TNTicketDeleteBatch")
    public String TNTicketDeleteBatch(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) {
        Map<String, Object> map = new HashMap<String, Object>();
        map = getTicketListByVO(map, validateProductListConditionVO);
        try {
            TNTicketCommodityDetailService.deleteLogByMap(map); //删除全部符合条件数据
        } catch (Exception e) {
            log.error("ValidateProductController...TNTicketDeleteBatch...method...deleteLogByMap...error", e);
        }

        PageInfo<TNTicket_CommodityDetail> page = null;
        List<TNTicket_CommodityDetail> TNTicket_CommodityDetailList = null;
        try {
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<TNTicket_CommodityDetail>(TNTicketCommodityDetailService.queryLogByMap(map));
            TNTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...TNTicketDeleteBatch...method...TNTicketCommodityDetailService...error", e);
        }
        model.addAttribute("page", page);
        model.addAttribute("TNTicket_CommodityDetailList", TNTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/TNValidateProdcuct";
    }

    @RequestMapping("TNTicketSaveBatch")
    public String TNTicketSaveBatch(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<TNTicket_CommodityDetail> page = null;
        List<TNTicket_CommodityDetail> TNTicket_CommodityDetailList = null;
        map = getTicketListByVO(map, validateProductListConditionVO);
        try {
            List<TNTicket_CommodityDetail> ticketList = TNTicketCommodityDetailService.queryLogByMap(map);
            if (null != ticketList && ticketList.size() > 0) {

                for (TNTicket_CommodityDetail ticket : ticketList) {

                    TNTicketCommodityItem TNTicketCommodityItem = TNTicketCommodityItemsService.selectByProdCommId(ticket.getProduct_id(), ticket.getCommodity_id());
                    if (null != TNTicketCommodityItem) {
                        //当价格校验失败表中的日期大于等于商品表中的日期时候才更新商品表中该条商品
                        if (TNTicketCommodityItem.getNew_grab_time() != null && DateUtil.compare_date(ticket.getCreate_time(), TNTicketCommodityItem.getNew_grab_time()) > 0) {
                            if (TNTicketCommodityItem.getCommodity_type().equals(ticket.getCommodity_type())) {    //确保 商品id 产品id 以及 商品类型 都相同
                                ticket.setDetail_id(TNTicketCommodityItem.getId());
                                TNTicketCommodityItemsService.updateByProdCommIdAndType(ticket);
                            }
                        }
                    }
                    // 入hbase
                    TNHBEntityOperation.saveTNTicketCommodity(ticket, ticket.getDetail_id(), Constant.TICKET_PRODUCT_H_TC, Constant.HB_VARIABLE_FAMILY);
                }
            }
            TNTicketCommodityDetailService.deleteLogByMap(map); //删除mysql库中符合条件数据
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<TNTicket_CommodityDetail>(TNTicketCommodityDetailService.queryLogByMap(map));
            TNTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...TicketSaveBatch...method...TCTicketCommodityDetailService...error", e);
        }
        model.addAttribute("page", page);
        model.addAttribute("TNTicket_CommodityDetailList", TNTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/TNValidateProdcuct";

    }


    @RequestMapping("QNRValidateProductList")
    public String QNRValidateProductList(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map = getTicketListByVO(map, validateProductListConditionVO);
        PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
        PageInfo<QNRTicket_CommodityDetail> page = new PageInfo<QNRTicket_CommodityDetail>(QNRTicketCommodityDetailService.queryLogByMap(map));
        List<QNRTicket_CommodityDetail> QNRTicket_CommodityDetailList = page.getList();
        model.addAttribute("page", page);
        model.addAttribute("QNRTicket_CommodityDetailList", QNRTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/QNRValidateProdcuct";

    }

    @RequestMapping(value = "/QNRTicketEdit", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> TicketEdit(ModelMap model, QNRTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<String, Object>(1);
        int code = 1;
        try {
            ticket = getQNRTicketByID(ticket);
        } catch (Exception e) {
            log.error("validateProduct...QNRTicketEdit memthod error", e);
            code = 0;
        }
        map.put("code", code);
        map.put("ticketVO", ticket);
        return map;

    }

    @RequestMapping("QNRTicketAdd")
    @ResponseBody
    public Map<String, Object> TNTicketAdd(ModelMap model, HttpServletRequest request, QNRTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        List<QNRTicket_CommodityDetail> QNRTicket_CommodityDetailList = null;
        PageInfo<QNRTicket_CommodityDetail> page = null;
        Date nowDate = new Date();
        try {
            if (StringTools.isValid(ticket.getDetail_id())) {
                ticket.setUpdate_time(nowDate);
                QNRTicketCommodityDetailService.updateLog(ticket);
            }
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            map = getTicketListByVO(map, validateProductListConditionVO);
            page = new PageInfo<QNRTicket_CommodityDetail>(QNRTicketCommodityDetailService.queryLogByMap(map));
            QNRTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("validateProduct...QNRTicketAdd memthod error", e);
            map.put("code", "-1");
        }
        model.addAttribute("page", page);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        model.addAttribute("QNRTicket_CommodityDetailList", QNRTicket_CommodityDetailList);
        return map;
    }

    @RequestMapping("deleteQNROneTicket")
    public String deleteQNROneTicket(ModelMap model, HttpServletRequest request, QNRTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        deleteQNRTicketByID(ticket);
        map = getTicketListByVO(map, validateProductListConditionVO);
        List<QNRTicket_CommodityDetail> QNRTicket_CommodityDetailList = null;
        PageInfo<QNRTicket_CommodityDetail> page = null;
        try {
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<QNRTicket_CommodityDetail>(QNRTicketCommodityDetailService.queryLogByMap(map));
            QNRTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...deleteQNROneTicket...method...queryLogByMap...error", e);
            map.put("code", "-1");
        }
        model.addAttribute("page", page);
        model.addAttribute("QNRTicket_CommodityDetailList", QNRTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/QNRValidateProdcuct";
    }


    @RequestMapping("saveQNROneTicket")
    @ResponseBody
    public Map<String, Object> saveQNROneTicket(ModelMap model, HttpServletRequest request, QNRTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        ticket = getQNRTicketByID(ticket);
        boolean isSave = false;
        try {
            QNRTicketCommodityItem QNRTicketCommodityItem = QNRTicketCommodityItemsService.selectByProdCommId(ticket.getProduct_id(), ticket.getCommodity_id());
            //当价格校验失败表中的日期大于等于商品表中的日期时候才更新商品表中该条商品
            if (null != QNRTicketCommodityItem) {
                if (QNRTicketCommodityItem.getNew_grab_time() != null && DateUtil.compare_date(ticket.getCreate_time(), QNRTicketCommodityItem.getNew_grab_time()) > 0) {
                    if (QNRTicketCommodityItem.getCommodity_type().equals(ticket.getCommodity_type())) {    //确保 商品id 产品id 以及 商品类型 都相同
                        QNRTicketCommodityItemsService.updateByProdCommIdAndType(ticket);
                    }
                }
            }

            isSave = QNRHBEntityOperation.saveQNRTicketCommodity(ticket, ticket.getDetail_id(), Constant.TICKET_PRODUCT_H_TC, Constant.HB_VARIABLE_FAMILY);
            if (isSave) {
                deleteQNRTicketByID(ticket);
            }
            map = getTicketListByVO(map, validateProductListConditionVO);
            List<QNRTicket_CommodityDetail> QNRTicket_CommodityDetailList = null;
            PageInfo<QNRTicket_CommodityDetail> page = null;
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<QNRTicket_CommodityDetail>(QNRTicketCommodityDetailService.queryLogByMap(map));
            QNRTicket_CommodityDetailList = page.getList();
            model.addAttribute("page", page);
            model.addAttribute("QNRTicket_CommodityDetailList", QNRTicket_CommodityDetailList);
        } catch (Exception e) {
            log.error("ValidateProductController...saveQNROneTicket...method..error ", e);
            map.put("code", "-1");
        }
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return map;
    }

    @RequestMapping("QNRTicketDeleteBatch")
    public String QNRTicketDeleteBatch(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) {
        Map<String, Object> map = new HashMap<String, Object>();
        map = getTicketListByVO(map, validateProductListConditionVO);
        try {
            QNRTicketCommodityDetailService.deleteLogByMap(map); //删除全部符合条件数据
        } catch (Exception e) {
            log.error("ValidateProductController...QNRTicketDeleteBatch...method...deleteLogByMap...error", e);
        }

        PageInfo<QNRTicket_CommodityDetail> page = null;
        List<QNRTicket_CommodityDetail> QNRTicket_CommodityDetailList = null;
        try {
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<QNRTicket_CommodityDetail>(QNRTicketCommodityDetailService.queryLogByMap(map));
            QNRTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...QNRTicketDeleteBatch...method...QNRTicketCommodityDetailService...error  ", e);
        }
        model.addAttribute("page", page);
        model.addAttribute("QNRTicket_CommodityDetailList", QNRTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/QNRValidateProdcuct";
    }

    @RequestMapping("QNRTicketSaveBatch")
    public String QNRTicketSaveBatch(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<QNRTicket_CommodityDetail> page = null;
        List<QNRTicket_CommodityDetail> QNRTicket_CommodityDetailList = null;
        map = getTicketListByVO(map, validateProductListConditionVO);
        try {
            List<QNRTicket_CommodityDetail> ticketList = QNRTicketCommodityDetailService.queryLogByMap(map);
            if (null != ticketList && ticketList.size() > 0) {

                for (QNRTicket_CommodityDetail ticket : ticketList) {

                    QNRTicketCommodityItem QNRTicketCommodityItem = QNRTicketCommodityItemsService.selectByProdCommId(ticket.getProduct_id(), ticket.getCommodity_id());
                    if (null != QNRTicketCommodityItem) {
                        //当价格校验失败表中的日期大于等于商品表中的日期时候才更新商品表中该条商品
                        if (QNRTicketCommodityItem.getNew_grab_time() != null && DateUtil.compare_date(ticket.getCreate_time(), QNRTicketCommodityItem.getNew_grab_time()) > 0) {
                            if (QNRTicketCommodityItem.getCommodity_type().equals(ticket.getCommodity_type())) {    //确保 商品id 产品id 以及 商品类型 都相同
                                QNRTicketCommodityItemsService.updateByProdCommIdAndType(ticket);
                            }
                        }
                    }
                    // 入hbase
                    QNRHBEntityOperation.saveQNRTicketCommodity(ticket, ticket.getDetail_id(), Constant.TICKET_PRODUCT_H_TC, Constant.HB_VARIABLE_FAMILY);
                }
            }
            QNRTicketCommodityDetailService.deleteLogByMap(map); //删除mysql库中符合条件数据
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<QNRTicket_CommodityDetail>(QNRTicketCommodityDetailService.queryLogByMap(map));
            QNRTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...TicketSaveBatch...method...QNRTicketCommodityDetailService...error", e);
        }
        model.addAttribute("page", page);
        model.addAttribute("QNRTicket_CommodityDetailList", QNRTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/QNRValidateProdcuct";

    }

    // TODO: 2017/10/11

    /**
     * 校验美团失败产品首页
     *
     * @param model
     * @param validateProductListConditionVO
     * @return
     * @throws Exception
     */
    @RequestMapping("MTValidateProductList")
    public String MTValidateProductList(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        map = getTicketListByVO(map, validateProductListConditionVO);
        PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
        PageInfo<MTTicket_CommodityDetail> page = new PageInfo<MTTicket_CommodityDetail>(MTTicketCommodityDetailService.queryLogByMap(map));
        List<MTTicket_CommodityDetail> MTTicket_CommodityDetailList = page.getList();
        model.addAttribute("page", page);
        model.addAttribute("MTTicket_CommodityDetailList", MTTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/MTValidateProdcuct";
    }

    @RequestMapping(value = "/MTTicketEdit", method = {RequestMethod.GET,
            RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> MTTicketEdit(ModelMap model, MTTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<String, Object>(1);
        int code = 1;
        try {
            ticket = getMTTicketByID(ticket);
        } catch (Exception e) {
            log.error("validateProduct...TicketEdit memthod error", e);
            code = 0;
        }
        map.put("code", code);
        map.put("ticketVO", ticket);
        return map;

    }


    @RequestMapping("MTTicketAdd")
    @ResponseBody
    public Map<String, Object> MTTicketAdd(ModelMap model, HttpServletRequest request, MTTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        List<MTTicket_CommodityDetail> MTTicket_CommodityDetailList = null;
        PageInfo<MTTicket_CommodityDetail> page = null;
        Date nowDate = new Date();
        try {
            if (StringTools.isValid(ticket.getDetail_id())) {
                ticket.setUpdate_time(nowDate);
                MTTicketCommodityDetailService.updateLog(ticket);
            }
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            map = getTicketListByVO(map, validateProductListConditionVO);
            page = new PageInfo<MTTicket_CommodityDetail>(MTTicketCommodityDetailService.queryLogByMap(map));
            MTTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("validateProduct...ticketAdd memthod error", e);
            map.put("code", "-1");
        }
        model.addAttribute("page", page);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        model.addAttribute("MTTicket_CommodityDetailList", MTTicket_CommodityDetailList);
        return map;
    }

    @RequestMapping("deleteMTOneTicket")
    public String deleteMTOneTicket(ModelMap model, HttpServletRequest request, MTTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        deleteMTTicketByID(ticket);
        map = getTicketListByVO(map, validateProductListConditionVO);
        List<MTTicket_CommodityDetail> MTTicket_CommodityDetailList = null;
        PageInfo<MTTicket_CommodityDetail> page = null;
        try {
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<MTTicket_CommodityDetail>(MTTicketCommodityDetailService.queryLogByMap(map));
            MTTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...deleteOneTicket...method...queryLogByMap...error", e);
            map.put("code", "-1");
        }
        model.addAttribute("page", page);
        model.addAttribute("MTTicket_CommodityDetailList", MTTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/MTValidateProdcuct";
    }

    @RequestMapping("saveMTOneTicket")
    @ResponseBody
    public Map<String, Object> saveMTOneTicket(ModelMap model, HttpServletRequest request, MTTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        String dateVO = request.getParameter("dateVO");
        String taskIDVO = request.getParameter("taskIDVO");
        ValidateProductListConditionVO validateProductListConditionVO = new ValidateProductListConditionVO();
        validateProductListConditionVO.setDateVO(dateVO);
        validateProductListConditionVO.setTaskIDVO(taskIDVO);
        ticket = getMTTicketByID(ticket);
        boolean isSave = false;
        try {
            MTTicketCommodityItem MTTicketCommodityItem = MTTicketCommodityItemsService.selectByProdCommId(ticket.getProduct_id(), ticket.getCommodity_id());
            //当价格校验失败表中的日期大于等于商品表中的日期时候才更新商品表中该条商品
            if (MTTicketCommodityItem != null) {
                if (MTTicketCommodityItem.getNew_grab_time() != null && DateUtil.compare_date(ticket.getCreate_time(), MTTicketCommodityItem.getNew_grab_time()) > 0) {
                    if (MTTicketCommodityItem.getCommodity_type().equals(ticket.getCommodity_type())) {    //确保 商品id 产品id 以及 商品类型 都相同
                        MTTicketCommodityItemsService.updateByProdCommIdAndType(ticket);
                    }
                }
            }
            isSave = MTHBEntityOperation.saveMTTicketCommodity(ticket, ticket.getDetail_id(), Constant.TICKET_PRODUCT_H_MT, Constant.HB_VARIABLE_FAMILY);
            if (isSave) {
                deleteMTTicketByID(ticket);
            }
            map = getTicketListByVO(map, validateProductListConditionVO);
            List<MTTicket_CommodityDetail> MTTicket_CommodityDetailList = null;
            PageInfo<MTTicket_CommodityDetail> page = null;
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<MTTicket_CommodityDetail>(MTTicketCommodityDetailService.queryLogByMap(map));
            MTTicket_CommodityDetailList = page.getList();
            model.addAttribute("page", page);
            model.addAttribute("MTTicket_CommodityDetailList", MTTicket_CommodityDetailList);
        } catch (Exception e) {
            log.error("ValidateProductController...saveOneTicket...method..error", e);
            map.put("code", "-1");
        }
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return map;
    }

    @RequestMapping("MTTicketDeleteBatch")
    public String MTTicketDeleteBatch(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) {
        Map<String, Object> map = new HashMap<String, Object>();
        map = getTicketListByVO(map, validateProductListConditionVO);
        try {
            MTTicketCommodityDetailService.deleteLogByMap(map); //删除全部符合条件数据
        } catch (Exception e) {
            log.error("ValidateProductController...TicketDeleteBatch...method...deleteLogByMap...error ", e);
        }

        PageInfo<MTTicket_CommodityDetail> page = null;
        List<MTTicket_CommodityDetail> MTTicket_CommodityDetailList = null;
        try {
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<MTTicket_CommodityDetail>(MTTicketCommodityDetailService.queryLogByMap(map));
            MTTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...TicketDeleteBatch...method...TCTicketCommodityDetailService...error", e);
        }
        model.addAttribute("page", page);
        model.addAttribute("MTTicket_CommodityDetailList", MTTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/MTValidateProdcuct";
    }

    @RequestMapping("MTTicketSaveBatch")
    public String MTTicketSaveBatch(ModelMap model, ValidateProductListConditionVO validateProductListConditionVO) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<MTTicket_CommodityDetail> page = null;
        List<MTTicket_CommodityDetail> MTTicket_CommodityDetailList = null;
        map = getTicketListByVO(map, validateProductListConditionVO);
        try {
            List<MTTicket_CommodityDetail> ticketList = MTTicketCommodityDetailService.queryLogByMap(map);
            if (null != ticketList && ticketList.size() > 0) {

                for (MTTicket_CommodityDetail ticket : ticketList) {

                    MTTicketCommodityItem MTTicketCommodityItem = MTTicketCommodityItemsService.selectByProdCommId(ticket.getProduct_id(), ticket.getCommodity_id());
                    if (MTTicketCommodityItem != null) {
                        //当价格校验失败表中的日期大于等于商品表中的日期时候才更新商品表中该条商品
                        if (MTTicketCommodityItem.getNew_grab_time() != null && DateUtil.compare_date(ticket.getCreate_time(), MTTicketCommodityItem.getNew_grab_time()) > 0) {
                            if (MTTicketCommodityItem.getCommodity_type().equals(ticket.getCommodity_type())) {    //确保 商品id 产品id 以及 商品类型 都相同
                                MTTicketCommodityItemsService.updateByProdCommIdAndType(ticket);
                            }
                        }
                    }
                    // 入hbase
                    MTHBEntityOperation.saveMTTicketCommodity(ticket, ticket.getDetail_id(), Constant.TICKET_PRODUCT_H_TC, Constant.HB_VARIABLE_FAMILY);
                }
            }
            MTTicketCommodityDetailService.deleteLogByMap(map); //删除mysql库中符合条件数据
            PageHelper.startPage(validateProductListConditionVO.getPage(), validateProductListConditionVO.getPageSize());
            page = new PageInfo<MTTicket_CommodityDetail>(MTTicketCommodityDetailService.queryLogByMap(map));
            MTTicket_CommodityDetailList = page.getList();
        } catch (Exception e) {
            log.error("ValidateProductController...TicketSaveBatch...method...TCTicketCommodityDetailService...error", e);
        }
        model.addAttribute("page", page);
        model.addAttribute("MTTicket_CommodityDetailList", MTTicket_CommodityDetailList);
        model.addAttribute("validateProductListConditionVO", validateProductListConditionVO);
        return "background/validateProduct/MTValidateProdcuct";

    }

    //TODO
    public void deleteTNTicketByID(TNTicket_CommodityDetail ticket) {
        if (StringTools.isValid(ticket.getDetail_id())) {
            try {
                TNTicketCommodityDetailService.deleteLogByID(ticket.getDetail_id());
            } catch (Exception e) {
                log.error("ValidateProductController...method...deleteTNTicketByID...error,detail_id=" + ticket.getDetail_id());
            }
        }
    }

    public void deleteXCTicketByID(XCTicket_CommodityDetail ticket) {
        if (StringTools.isValid(ticket.getDetail_id())) {
            try {
                XCTicketCommodityDetailService.deleteLogByID(ticket.getDetail_id());
            } catch (Exception e) {
                log.error("ValidateProductController...method...deleteXCTicketByID...error,detail_id=" + ticket.getDetail_id());
            }
        }
    }

    public void deleteQNRTicketByID(QNRTicket_CommodityDetail ticket) {
        if (StringTools.isValid(ticket.getDetail_id())) {
            try {
                QNRTicketCommodityDetailService.deleteLogByID(ticket.getDetail_id());
            } catch (Exception e) {
                log.error("ValidateProductController...method...deleteQNRTicketByID...error,detail_id=" + ticket.getDetail_id());
            }
        }
    }


    public String getRowKey(TCTicket_CommodityDetail ticket) {
        String rowKey = "";
        if (StringTools.isValid(ticket.getCommodity_id()) && null != ticket.getCreate_time()
                && StringTools.isValid(ticket.getTask_id())) {
            rowKey = RowkeyGenerator.generateRowkey(ticket.getCommodity_id(), ticket.getCreate_time(), ticket.getTask_id());
        }
        return rowKey;
    }

    public TCTicket_CommodityDetail getTCTicketByID(TCTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        if (StringTools.isValid(ticket.getDetail_id())) {
            map.put("detail_id", ticket.getDetail_id());
            List<TCTicket_CommodityDetail> ticketList = null;
            try {
                ticketList = TCTicketCommodityDetailService.getTCTicket_CommodityDetailLogListByID(map);
            } catch (Exception e) {
                log.error("ValidateProductController...getTicketByID...method...error,detail_id=" + ticket.getDetail_id());
            }
            if (null != ticketList && ticketList.size() > 0) {
                ticket = ticketList.get(0);
            }
        }
        return ticket;
    }

    public void deleteTCTicketByID(TCTicket_CommodityDetail ticket) {
        if (StringTools.isValid(ticket.getDetail_id())) {
            try {
                TCTicketCommodityDetailService.deleteLogByID(ticket.getDetail_id());
            } catch (Exception e) {
                log.error("ValidateProductController...method...deleteTicketByID...error,detail_id=" + ticket.getDetail_id());
            }
        }
    }


    public Map<String, Object> getTicketListByVO(Map<String, Object> map, ValidateProductListConditionVO validateProductListConditionVO) {

        if (StringTools.isValid(validateProductListConditionVO.getTaskIDVO()) && validateProductListConditionVO.getDateVO().equals("taskID")) {
            map.put("task_id", validateProductListConditionVO.getTaskIDVO());
            log.info("validateProduct controller getTicketListByVO method  task_id=" + validateProductListConditionVO.getTaskIDVO());
        }
        Date endDate = new Date();
        Date startDate = null;
        if (StringTools.isValid(validateProductListConditionVO.getDateVO())) {
            String dateVO = validateProductListConditionVO.getDateVO();
            switch (dateVO) {
                case Constant.DATA_TIME_YEAR:
                    startDate = DateUtil.getDateBeforeYear(endDate);
                    break;
                case Constant.DATA_TIME_QUARTER:
                    startDate = DateUtil.getDateBeforeQuarter(endDate);
                    break;
                case Constant.DATA_TIME_MONTH:
                    startDate = DateUtil.getDateBeforeMonth(endDate);
                    break;
                case Constant.DATA_TIME_WEEK:
                    startDate = DateUtil.getDateBeforeWeek(endDate);
                    break;
                default:
                    break;
            }
        } else if (!StringTools.isValid(validateProductListConditionVO.getDateVO())
                && !StringTools.isValid(validateProductListConditionVO.getTaskIDVO())) {
            startDate = DateUtil.getDateBeforeMonth(endDate); //默认取一个月的数据
        }
        map.put("start_date", startDate);
        map.put("end_date", endDate);
        log.info("validateProduct controller getTicketListByVO method  start_date=" + startDate);
        return map;
    }

    public TNTicket_CommodityDetail getTNTicketByID(TNTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        if (StringTools.isValid(ticket.getDetail_id())) {
            map.put("detail_id", ticket.getDetail_id());
            List<TNTicket_CommodityDetail> ticketList = null;
            try {
                ticketList = TNTicketCommodityDetailService.getTNTicket_CommodityDetailLogListByID(map);
            } catch (Exception e) {
                log.error("ValidateProductController...getTNTicketByID...method...error,detail_id=" + ticket.getDetail_id());
            }
            if (null != ticketList && ticketList.size() > 0) {
                ticket = ticketList.get(0);
            }
        }
        return ticket;
    }

    public XCTicket_CommodityDetail getXCTicketByID(XCTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        if (StringTools.isValid(ticket.getDetail_id())) {
            map.put("detail_id", ticket.getDetail_id());
            List<XCTicket_CommodityDetail> ticketList = null;
            try {
                ticketList = XCTicketCommodityDetailService.getXCTicket_CommodityDetailLogListByID(map);
            } catch (Exception e) {
                log.error("ValidateProductController...getXCTicketByID...method...error,detail_id=" + ticket.getDetail_id());
            }
            if (null != ticketList && ticketList.size() > 0) {
                ticket = ticketList.get(0);
            }
        }
        return ticket;
    }

    public QNRTicket_CommodityDetail getQNRTicketByID(QNRTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        if (StringTools.isValid(ticket.getDetail_id())) {
            map.put("detail_id", ticket.getDetail_id());
            List<QNRTicket_CommodityDetail> ticketList = null;
            try {
                ticketList = QNRTicketCommodityDetailService.getQNRTicket_CommodityDetailLogListByID(map);
            } catch (Exception e) {
                log.error("ValidateProductController...getXCTicketByID...method...error,detail_id=" + ticket.getDetail_id());
            }
            if (null != ticketList && ticketList.size() > 0) {
                ticket = ticketList.get(0);
            }
        }
        return ticket;
    }

    public MTTicket_CommodityDetail getMTTicketByID(MTTicket_CommodityDetail ticket) {
        Map<String, Object> map = new HashMap<>();
        if (StringTools.isValid(ticket.getDetail_id())) {
            map.put("detail_id", ticket.getDetail_id());
            List<MTTicket_CommodityDetail> ticketList = null;
            try {
                ticketList = MTTicketCommodityDetailService.getMTTicket_CommodityDetailLogListByID(map);
            } catch (Exception e) {
                log.error("ValidateProductController...getTicketByID...method...error,detail_id=" + ticket.getDetail_id());
            }
            if (null != ticketList && ticketList.size() > 0) {
                ticket = ticketList.get(0);
            }
        }
        return ticket;
    }

    public void deleteMTTicketByID(MTTicket_CommodityDetail ticket) {
        if (StringTools.isValid(ticket.getDetail_id())) {
            try {
                MTTicketCommodityDetailService.deleteLogByID(ticket.getDetail_id());
            } catch (Exception e) {
                log.error("ValidateProductController...method...deleteTicketByID...error,detail_id=" + ticket.getDetail_id());
            }
        }
    }


}
